package com.yloveg.controller;

import java.util.Map;

import org.apache.commons.collections4.map.HashedMap;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yloveg.base.BaseController;
import com.yloveg.common.PageUtil;
import com.yloveg.entity.ProductContent;
import com.yloveg.entity.SView;
import com.yloveg.entity.ShopInfo;
import com.yloveg.service.ProductContentService;
import com.yloveg.service.SViewService;
import com.yloveg.service.ShopInfoService;

@Controller
@RequestMapping("/free")
public class FreeMainController extends BaseController {
    private final Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private ProductContentService productContentService;
    
    @Autowired
    private ShopInfoService shopInfoService;
    
    @Autowired
    private SViewService sViewService;
    
    
    @RequestMapping({"{shopId}/index"}) 
    public String index(Integer pageNo,ProductContent product,ModelMap map){
    	String style = "1";
    	if(pageNo==null){
    		pageNo=1;
    	}
    	product.setPublisherState("1");
    	map = PageUtil.freemarkPage(map,productContentService.queryProductContentPage(pageNo,6,product));
    	map.put("productName", product.getProductName());
        //店铺信息
    	ShopInfo info = new ShopInfo();	
    	info.setId(product.getShopId());
    	info = shopInfoService.queryOneShopInfo(info);
    	if(info==null){
    		info = new ShopInfo();	
    	}
    	style = info.getShopStyle();
    	if(style==null){
    		style="1";
    	}
    	setSessionShop(info);
    	map.put("shopInfo", info);
        return "shopShow/view"+style+"/index";
    }
    
    @RequestMapping({"productDetial"}) 
    public String productDetial(Integer id,ModelMap map){
    	String style = "0";
    	if(id!=null){
        	ProductContent product = productContentService.selectByPrimaryKey(Long.valueOf(id)); 
        	if(product.getBrowseNumber()!=null){
        		product.setBrowseNumber(product.getBrowseNumber()+1);
        	}else{
        		product.setBrowseNumber(1L);
        	}
        	productContentService.updateProduct(product);
        	map.put("product", product);
        	ShopInfo info = new ShopInfo();
        	map.put("info", info);
        	info.setId(product.getShopId());
        	info = shopInfoService.queryOneShopInfo(info);
        	map.put("shopInfo", info);
        	style = info.getShopStyle();
    	}else{
    		map.put("product", new ProductContent());
    		map.put("shopInfo", new ShopInfo());
    	}
    	
        return "shopShow/view"+style+"/productDetial";
    }
    
    @RequestMapping({"{shopId}/productList"}) 
    public String productList(ProductContent product,ModelMap map){
    	String style = "0";
        //店铺信息
    	ShopInfo info = new ShopInfo();	
    	info.setId(product.getShopId());
    	info = shopInfoService.queryOneShopInfo(info);
    	if(info==null){
    		info = new ShopInfo();	
    	}
    	style = info.getShopStyle();
    	setSessionShop(info);
    	map.put("shopInfo", info);
    	return "shopShow/view"+style+"/productList";
    }
    
    @RequestMapping("/queryProductContentPage")
    @ResponseBody
    public Map<String, Object> queryProductContentPage(int page, int limit,ProductContent product){
    	if(product.getShopId()!=null) {
            Map<String, Object> map = PageUtil.dateToLayTable(productContentService.queryProductContentPage(page,limit,product));
            return map;
    	}else {
    		product.setShopId(-1l);
            Map<String, Object> map = PageUtil.dateToLayTable(productContentService.queryProductContentPage(page,limit,product));
            return map;
    	}
        
    }
    @RequestMapping({"main"}) 
    public String shopList(ShopInfo shop,ModelMap map){
    	return "shopShow/main";
    }
    
    @RequestMapping("/queryShopInfoPage")
    @ResponseBody
    public Map<String, Object> queryShopInfoPage(int page, int limit,ShopInfo shop){
        Map<String, Object> map = PageUtil.dateToLayTable(shopInfoService.queryProductContentShopInfoPage(page,limit,shop));
        return map;      
    }
    
    @RequestMapping({"/yjfk"}) 
    public String yjfk(ModelMap map){
        return "shopShow/yjfk";
    }
    
    /**
     * 保存意见反馈
     * 
     */
 
    @RequestMapping("/saveYjfk")
    @ResponseBody
    public Map<String, Object> saveYjfk (SView sview) {
    	Map<String, Object> map = new HashedMap<String, Object>();
    	int result = sViewService.insert(sview);
		if(result>0) {
			map.put("code", "1");
			map.put("msg", "保存成功！");				
		}else {
			map.put("code", "0");
			map.put("msg", "请联系管理员！");	
		}
    	return map;
    }
    
}
